package com.htthorizon.ws.webservice.service.impl;

import com.htthorizon.common.core.domain.R;
import com.htthorizon.system.api.RemoteUserService;
import com.htthorizon.system.api.domain.MonitorAll;
import com.htthorizon.ws.webservice.dto.Api;
import com.htthorizon.ws.webservice.dto.ApiReturn;
import com.htthorizon.ws.webservice.dto.GetKpiValue;
import com.htthorizon.ws.webservice.dto.GetKpiValueResponse;
import com.htthorizon.ws.webservice.enu.KpiValue;
import com.htthorizon.ws.webservice.service.WSService;
import com.htthorizon.ws.webservice.utils.XmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.jws.WebService;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Optional;

/**
 * @author Brilliant
 */
@WebService(//对外发布的服务名
        targetNamespace = "http://service.webservice.ws.htthorizon.com/"//指定你想要的名称空间，通常使用使用包名反转
       )
@Component
public class WSServiceImpl implements WSService {

    @Autowired
    private RemoteUserService remoteUserService;
    @Override
    public String getKPIValue(String param) {
        System.out.println("ws:param:"+param);
        param="<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" +
                "<info>\n" +
                "<CorporationCode>16</CorporationCode>\n" +
                "<!--传入的指标名称可以为一个或多个,BusinessSystemLoginRoll和BusinessSystemOnlineRoll只能单独调用-->\n" +
                "<Time>" + "" + "</Time>\n" +
                "<api name=\"BusinessUserRegNum\"></api>\n" +
                "<api name=\"BusinessDataTableSpace\"></api>\n" +
                "<api name=\"BusinessSystemOnlineNum\"></api>\n" +
                "<api name=\"BusinessDayLoginNum\"></api>\n" +
                "<api name=\"BusinessVisitCount\"></api>\n" +
                "<api name=\"BusinessSystemSessionNum\"></api>\n" +
                "<api name=\"BusinessSystemResponseTime\"></api>\n" +
                "<api name=\"BusinessSystemRunningTime\"></api>\n" +
                "<api name=\"BusinessSystemDBTime\"></api>\n" +
                "</info>";

        GetKpiValueResponse getKpiValueResponse = new GetKpiValueResponse();
        GetKpiValue value = (GetKpiValue) XmlUtil.convertXmlStrToObject(GetKpiValue.class,param);
        List<Api> api = value.getApi();
        List<ApiReturn> resultList = new ArrayList<ApiReturn>();
        if(!CollectionUtils.isEmpty(api)){
            for(Api info :api){
                try{
                    String res = execQuery(info.getName());
                    ApiReturn apiReturn = new ApiReturn();
                    apiReturn.setName(info.getName());
                    apiReturn.setValue(res);
                    resultList.add(apiReturn);
                }catch (Exception e){
                    getKpiValueResponse.setStatus("failure");
                    getKpiValueResponse.setMessage("执行失败");
                    getKpiValueResponse.setReason(e.toString());
                    String xmlStr = XmlUtil.convertToXml(getKpiValueResponse);
                    return xmlStr;
                }

            }
        }
        //拼接xml
        getKpiValueResponse.setApi(resultList);
        getKpiValueResponse.setStatus("success");
        getKpiValueResponse.setMessage("执行成功");
        String xmlStr = XmlUtil.convertToXml(getKpiValueResponse);
        System.out.println(xmlStr);
        return xmlStr;
    }


    private String execQuery(String name) {
        R<MonitorAll> all = remoteUserService.all();
        MonitorAll monitorAll = Optional.ofNullable(all).map(R::getData).orElse(new MonitorAll());

        if(KpiValue.BUSINESSSYSTEMONLINENUM.getCode().equals(name)|| KpiValue.BUSINESSSYSTEMSESSIONNUM.getCode().equals(name)){
            return monitorAll.getBusinessSystemOnlineNum().toString();
        }
        if(KpiValue.BUSINESSUSERREGNUM.getCode().equals(name)){
            return monitorAll.getBusinessUserRegNum().toString();
        }
        if(KpiValue.BUSINESSVISITCOUNT.getCode().equals(name)){
            return monitorAll.getBusinessVisitCount().toString();
        }
        if(KpiValue.BUSINESSDATATABLESPACE.getCode().equals(name)){
            return monitorAll.getBusinessDataTableSpace().toString();
        }
        if(KpiValue.BUSINESSDAYLOGINNUM.getCode().equals(name)){
            return monitorAll.getBusinessDayLoginNum().toString();
        }
        if(KpiValue.BUSINESSSYSTEMRESPONSETIME.getCode().equals(name)){
            return monitorAll.getBusinessSystemResponseTime().toString();
        }
        if(KpiValue.BUSINESSSYSTEMRUNNINGTIME.getCode().equals(name)){
            return monitorAll.getBusinessSystemRunningTime().toString();
        }
        if(KpiValue.BUSINESSSYSTEMDBTIME.getCode().equals(name)){
            return monitorAll.getBusinessSystemDBTime().toString();
        }
        return null;
    }


    public String setSize(int size) {
        //获取到的size为：1705230
        int GB = 1024 * 1024 * 1024;//定义GB的计算常量
        int MB = 1024 * 1024;//定义MB的计算常量
        int KB = 1024;//定义KB的计算常量
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数
        String resultSize  = df.format(size / (float) MB);;

        /*if (size / GB >= 1) {
            //如果当前Byte的值大于等于1GB
            resultSize = df.format(size / (float) GB) + "GB   ";
        } else if (size / MB >= 1) {
            //如果当前Byte的值大于等于1MB
            resultSize = df.format(size / (float) MB) + "MB   ";
        } else if (size / KB >= 1) {
            //如果当前Byte的值大于等于1KB
            resultSize = df.format(size / (float) KB) + "KB   ";
        } else {
            resultSize = size + "B   ";
        }*/
        return resultSize;
    }
}
